import wx
from zope import interface
import os, sys

APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

from watch_builder.gui.watchobjects import interfaces as watch_interfaces
from watch_builder.gui.watchobjects.base import SimpleWatcherDataObject
from watch_builder.gui import utils


class ExecutePathDataObject(SimpleWatcherDataObject):
    interface.implements(watch_interfaces.IExecutePathDataObject)

   
    def __init__(self, *args, **kwargs):
        super(ExecutePathDataObject, self).__init__(*args, **kwargs)
        self.buildDefaults()
        self.setImage(kwargs.get('image', self._default_image))
        # set all kwarg attrs relevant to building the object's xml
        for (attr, xattr) in self._xml_build_attrs:
            setattr(self, attr, kwargs.get(attr, ''))
        self._children = []
        self.build_schema()


    def buildDefaults(self):
        # default image
        self._default_image = "execute_path_object.gif"
        # attributes that can be read to and from xml
        self._xml_build_attrs = [('executePathName','//execute_path/@name'), ('packageName', '//execute_path/@package' ), ('klass', '//execute_path/@klass')]
     
        # valid children, embedded resources, and only child for self.schema
        self._valid_children = [watch_interfaces.IDecisionDataObject, watch_interfaces.IActionDataObject]
        self._embedded_resources = [watch_interfaces.ICommentDataObject]
        self._only_child = [watch_interfaces.IDecisionDataObject]


    def guiObjXml(self):
        for attr, xattr in self._xml_build_attrs:
            if not hasattr(self, attr):
                setattr(self, attr, "")
        xml = """
        <execute_path
            name="%(executePathName)s"
            package="%(packageName)s"
            klass="%(klass)s"
            >
        </execute_path>
        """ % self.__dict__
        return xml
        

    def watchBuilderXml(self):
        for attr, xattr in self._xml_build_attrs:
            if not hasattr(self, attr):
                setattr(self, attr, "")
        xml_dict = {}
        xml_dict.update(self.__dict__)
        xml_dict['decision'] = ''
        xml_dict['actions'] = ''
        actions = []
        for child in self.__children:
            if watch_interfaces.IDecisionDataObject.implementedBy(child):
                xml_dict['decision'] = "<decision>%s</decision>" % child.decisionName
            elif watch_interfaces.IActionDataObject.implementedBy(child):
                child_xml = """
                """
                actions.append(child_xml)
        for action in actions:
            axml = ''
            # solve this later, it doesn't matter right now
            xml_dict['actions'] += axml
                
        xml = """
        <execute_path
            name="%(executePathNamse)s"
            package="%(packageName)s"
            klass="%(klass)s"
            >
            <path>
              %(decision)s
              %(actions)s
            </path>
        </execute_path>
        """ % xml_dict
        return xml

